package com.hs.rabbitmqdemo.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RabbitMQConfig
 * @Description TODO RabbitMQ的配置类
 * @Author 郭敖
 * @Date 2023年09月25日 21:42
 * @Version 1.0
 */
@Configuration
public class RabbitMQConfig {
    //交换机名称
    public static final String ITEM_TOPIC_EXCHANGE = "item_topic_exchange";
    //队列名称
    public static final String ITEM_QUEUE = "item_queue";

    //声明交换机
    @Bean("itemTopicExchange")
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();
    }

    @Bean("fanoutExchange")
    public Exchange fanoutExchange(){
        return ExchangeBuilder.fanoutExchange("hs").durable(true).build();
    }

    //声明队列
    @Bean("itemQueue")
    public Queue itemQueue(){
        return QueueBuilder.durable(ITEM_QUEUE).build();
    }

    //声明订阅者队列
    @Bean("fanout1")
    public Queue getQueue1(){
        return QueueBuilder.durable("fanout1").build();
    }

    @Bean("fanout2")
    public Queue getQueue2(){
        return QueueBuilder.durable("fanout2").build();
    }

    //绑定队列和交换机
    @Bean
    public Binding itemQueueExchange(@Qualifier("itemQueue") Queue queue,
                                     @Qualifier("itemTopicExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs();
    }

    @Bean
    public Binding fanoutExchange1(@Qualifier("fanout1")Queue queue,@Qualifier("fanoutExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("hs").noargs();
    }

    @Bean
    public Binding foutExchange2 (@Qualifier("fanout2")Queue queue,@Qualifier("fanoutExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("hs").noargs();
    }

}
